package com.zzgc.common.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 *  读取配置文件, 单例模式
 * @ClassName ReadConfig
 * @Date 2022/11/16  15:27
 * @Version 1.0
 */
public class ReadConfig {

	// private static Logger log = Logger.getLogger(IConfig.class);
	/**
	 * 同步锁
	 */
	private static final Object obj = new Object();

	/**
	 * 配置文件
	 */
	private static Properties prop = null;

	/**
	 * 配置对象单例模式
	 */
	private static ReadConfig config = null;

	/**
	 * 配置文件名称
	 */
	private final static String FILE_NAME = "/application.properties";

	static {
		prop = new Properties();
		try {
			//prop.load(ReadConfig.class.getResourceAsStream(FILE_NAME));
            //动态加载properties值
            String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
            InputStream is = new FileInputStream(path + "/application.properties");
            prop.load(is);
		} catch (IOException e) {
			//LoggerUtils.fmtError(ReadConfig.class, e, "加载文件异常，文件路径：%s", FILE_NAME);
            System.out.println("加载配置文件异常, 文件路径: "+FILE_NAME);
		}

	}

	/**
	 * 获取单例模式对象实例
	 *  双重锁
	 * @return 唯一对象实例
	 */
	public static ReadConfig getInstance() {
		if (null == config) {
			synchronized (obj) {
                if (null == config) {
                    config = new ReadConfig();
                }
			}
		}
		return config;
	}

	/**
	 */
	public static String get(String key) {
		return prop.getProperty(key);
	}

}
